package cn.xeblog.plugin.factory;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.GlobalThreadPool;
import cn.xeblog.commons.util.ThreadUtils;
import cn.xeblog.plugin.action.InputAction;
import cn.xeblog.plugin.cache.DataCache;
import cn.xeblog.plugin.listener.ProjectCloseListener;
import cn.xeblog.plugin.ui.MainWindow;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.RegisterToolWindowTask;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.ex.ToolWindowManagerListener;
import com.intellij.ui.AncestorListenerAdapter;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import java.io.InputStream;

import static cn.xeblog.plugin.listener.ProjectCloseListener.WINDOW_ID;

/**
 * @author anlingyi
 * @date 2020/5/26
 */
public class MainWindowFactory implements ToolWindowFactory, ToolWindowManagerListener {

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        DataCache.project = project;
        JPanel mainPanel = MainWindow.getInstance().getMainPanel();
        ContentFactory contentFactory = ContentFactory.getInstance();
        Content content = contentFactory.createContent(mainPanel, "", false);
        toolWindow.getContentManager().addContent(content);
        DataCache.toolWindow = toolWindow;
    }

    @Override
    public void toolWindowShown(@NotNull ToolWindow toolWindow) {
        if (!WINDOW_ID.equals(toolWindow.getId())) {
            return;
        }
        // 要点击两次才能正常过来，毕竟不懂为什么
        Project project = toolWindow.getProject();
        if (project == DataCache.project) {
            return;
        }
        if (DataCache.toolWindow != null) {
            DataCache.toolWindow.hide();
        }
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        toolWindowManager.invokeLater(() -> {
            try {
                Icon icon = toolWindow.getIcon();
                if (icon == null) {
                    InputStream inputStream = ProjectCloseListener.class.getResourceAsStream("/images/logo.png");
                    icon = new ImageIcon(IoUtil.readBytes(inputStream));
                }
                RegisterToolWindowTask xeChat = RegisterToolWindowTask.notClosable(WINDOW_ID, icon);
                toolWindow.remove();
                // TODO can't calculate origin icon (too deep in hierarchy), src: com.intellij.openapi.wm.impl.ToolWindowIcon@6a8b6070
                ToolWindow toolWindow2 = toolWindowManager.registerToolWindow(xeChat);
                new MainWindowFactory().createToolWindowContent(project, toolWindow2);
                toolWindow2.show();
            } catch (Exception e) {
            }
        });
    }
}
